package com.kede.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kede.dto.AjaxMessage;
import com.kede.dto.LayuiTableData;
import com.kede.entity.Ctrain;
import com.kede.service.CtrainService;
import com.kede.service.MenuService;
import com.kede.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 【请填写作者】
 * @since 2020-04-16
 */
@Controller
@RequestMapping("/skill")
public class CtrainController {
    @Autowired
    MenuService menuService;
    @Autowired
    CtrainService ctrainService;
    @Autowired
    UserService userService;

    @GetMapping({"/ctrain"})
    public String ctrain(String id,
                         Model model){
        model.addAttribute("mid",id);
        return "skill/ctrain";
    }

    @ResponseBody
    @GetMapping("/ctrainList")
    public LayuiTableData ctrainList(
            String mid,
            String title,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "limit",defaultValue = "10") Integer limit){

        QueryWrapper<Ctrain> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(title)){
            queryWrapper.like("title",title);
        }else{
            queryWrapper.eq("mid",mid);
        }
        queryWrapper.eq("uid",1);

        Page<Ctrain> rolePage = new Page<>();
        rolePage.setCurrent(page);
        rolePage.setSize(limit);

        Page<Ctrain> ctrainRlt = ctrainService.page(rolePage,queryWrapper);
        LayuiTableData layuiTableData = new LayuiTableData();
        List<Ctrain> ctrains = new ArrayList<Ctrain>();
        for (Ctrain ctrain :ctrainRlt.getRecords()){
            ctrain.setUid(ctrainService.getById(ctrain.getUid()).getUid());
            ctrains.add(ctrain);
        }
        if(ctrainRlt.getRecords() == null){
            layuiTableData.setCount(1L);
            layuiTableData.setMsg("Fail");
            return layuiTableData;
        }else {
            layuiTableData.setCode(0L);
            layuiTableData.setCount(ctrainRlt.getTotal());
            layuiTableData.setData(ctrains);
            return layuiTableData;
        }
    }

    @GetMapping("/c_add")
    public String ctrainAdd(
            String mid,
            Model model) {
        model.addAttribute("mid",mid);
        model.addAttribute("ctrain",new Ctrain());
        return "skill/cadd";
    }

    @ResponseBody
    @PostMapping("c_date")
    public AjaxMessage updatebp(Ctrain ctrain){
        AjaxMessage message = new AjaxMessage();
        long uid =Long.parseLong("1");
        ctrain.setUid(uid);
        if (ctrainService.save(ctrain)){
            message.setCode(0);
        }else {
            message.setCode(1);
            message.setMsg("更新失败");
        }
        return message;
    }

    @GetMapping("/Edit/{id}")
    public ModelAndView Edit(@PathVariable("id") Integer id){
        Ctrain ctrain = ctrainService.getById(id);
        ModelAndView modelAndView = new ModelAndView("skill/cedit");
        modelAndView.addObject("ctrain",ctrain);
        return modelAndView;
    }

    @ResponseBody
    @PostMapping("/ctrainUpdate")
    public AjaxMessage ctrainUpdate(Ctrain ctrain) {
        AjaxMessage ajaxMessage = new AjaxMessage();
        boolean b = ctrainService.updateById(ctrain);
        if (b == true) {
            ajaxMessage.setCode(0);
            return ajaxMessage;
        } else {
            ajaxMessage.setCode(1);
            ajaxMessage.setMsg("Fail");
            return ajaxMessage;
        }
    }
}
